//////////////////////////////////////////////////////////////////// Survey create/edit
// Model
var SurveyFieldOrder = Backbone.Model.extend({
  idAttribute: 'order'
});

var SurveyField = Backbone.Model.extend({
  initialize: function() {
    this.orders = new SurveyFieldOrderList(this.get('orders'));
  },
  parse: function(response, options) {
    if (response && response.orders) {
      response.orders = new SurveyFieldOrderList(response.orders);
    }
    return response;
  }
});

var SurveyModel = Backbone.Model.extend({

  urlRoot : '/survey',

  defaults: {
    title: '',
    description: '',
    predefined_value: '',
    required: 0,
    other: 0,
    fields: []
  },

  initialize: function() {
    this.fields = new SurveyFieldList(this.get('fields'));
  },

  parse: function(response, options) {
    if (response && response.data && response.data.survey) {
      response.fields = new SurveyFieldList(response.fields);
    }

    return response;
  },

  setValues: function(values) {
    var fields = [];
    if (_.isArray(values)) {
      _.each(values, function(value, idx){
        if (value) {
          var field = {
            title: value.substr(0, 255),
            checked: 0
          };
          fields.push(field);
        }
      });
    }
    if (fields.length == 0) {
      fields.push({
        title: '',
        checked: 0
      });
    }
    this.set('fields', fields, {from: 'batch'});
  }
});

var Survey = Backbone.Model.extend({
  initialize: function(options) {
    this._id = options.id;
    this._event_id = options.event_id;
  },
  parse: function(response, options) {
    if (response && response.data && response.data.survey) {
      var survey = response.data.survey;
      this.questions = new SurveyModelList(survey.models);
      return survey;
    }

    return response;
  },
  url : function() {
    return '/survey/result.do?event_id=' + this._event_id + '&survey_id=' + this._id;
  }
});


////////////////////////////////////////////////////////////////////
// Collection
var SurveyFieldOrderList = Backbone.Collection.extend({
  model: SurveyFieldOrder,
  comparator: function(property) {
    //console.log(arguments);
    //if (a.get('total') == b.get('total')) return 0;
    //return a.get('total') > b.get('total') ? -1 : 1;
    //return selectedStrategy.apply(myModel.get(property));
  },
  strategies: {
    order: function (model) { return model.get("order"); },
    total: function (model) { return model.get("total"); },

    orderReverse: function (model) { return -model.get("order"); },
    totalReverse: function (model) { return -model.get("total"); }
  },
  changeSort: function (sortProperty) {
    this.comparator = this.strategies[sortProperty];
  },
  sortByIndex: function(direction) {
    if (typeof direction == 'undefined' || direction == 'DESC') {
      this.changeSort('orderReverse');
    } else {
      this.changeSort('order');
    }
    return this.sort();
  },
  sortByTotal: function(direction) {
    if (typeof direction == 'undefined' || direction == 'DESC') {
      this.changeSort('totalReverse');
    } else {
      this.changeSort('total');
    }
    return this.sort();
  },
  initialize: function () {
    this.changeSort("order");
  }
});

var SurveyFieldList = Backbone.Collection.extend({
  model: SurveyField,

  comparator: function(property) {
    //console.log(arguments);
    //if (a.get('total') == b.get('total')) return 0;
    //return a.get('total') > b.get('total') ? -1 : 1;
    //return selectedStrategy.apply(myModel.get(property));
  },
  strategies: {
    total: function (model) { return model.get("count"); },
    totalReverse: function (model) { return -model.get("count"); }
  },
  changeSort: function (sortProperty) {
    this.comparator = this.strategies[sortProperty];
  },
  sortByTotal: function(direction) {
    if (typeof direction == 'undefined' || direction == 'DESC') {
      this.changeSort('totalReverse');
    } else {
      this.changeSort('total');
    }
    return this.sort();
  },
  initialize: function () {
    this.changeSort("totalReverse");
  }
});

var SurveyModelList = Backbone.Collection.extend({
  model: SurveyModel
});

var SurveyList = Backbone.Collection.extend({
  url: '/survey/list.do',
  model: Survey,
  initialize: function(models, options) {
    this.title = options ? options.title : '';
    this.description = options ? options.description : '';
    this.type = options ? options.type : 0;
  },

  parse: function(response, options) {
    if (response && response.data && response.data.list) {
      if (typeof response.data.login != 'undefined') this.login = response.data.login;
      if (typeof response.data.page != 'undefined') this.page = response.data.page;
      if (typeof response.data.pages != 'undefined') this.pages = response.data.pages;
      if (typeof response.data.total != 'undefined') this.total = response.data.total;
      return response.data.list;
    }
    return response;
  },

  getData: function() {
    var data = {
      title: this.title,
      description: this.description,
      type: this.type ? this.type : 0,
      models: this.toJSON()
    };
    return data;
  },

  getList: function(event_id, page, each) {
    if (event_id) this._event_id = event_id;
    if (typeof page == 'undefined') page = 0;
    if (typeof each == 'undefined') each = 10;
    var self = this;
    var options = {
      url: this.url,
      data: {
        event_id: this._event_id,
        backend: 1,
        page: page,
        each: each
      }
    };
    return this.fetch(options);
  }
});
